package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CmsArticle;
import com.ruoyi.system.service.ICmsArticleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 文章 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-29
 */
@Controller
@RequestMapping("/system/cmsArticle")
public class CmsArticleController extends BaseController
{
    private String prefix = "system/cmsArticle";
	
	@Autowired
	private ICmsArticleService cmsArticleService;
	
	@RequiresPermissions("system:cmsArticle:view")
	@GetMapping()
	public String cmsArticle()
	{
	    return prefix + "/cmsArticle";
	}
	
	/**
	 * 查询文章列表
	 */
	@RequiresPermissions("system:cmsArticle:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(CmsArticle cmsArticle)
	{
		startPage();
        List<CmsArticle> list = cmsArticleService.selectCmsArticleList(cmsArticle);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出文章列表
	 */
	@RequiresPermissions("system:cmsArticle:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsArticle cmsArticle)
    {
    	List<CmsArticle> list = cmsArticleService.selectCmsArticleList(cmsArticle);
        ExcelUtil<CmsArticle> util = new ExcelUtil<CmsArticle>(CmsArticle.class);
        return util.exportExcel(list, "cmsArticle");
    }
	
	/**
	 * 新增文章
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存文章
	 */
	@RequiresPermissions("system:cmsArticle:add")
	@Log(title = "文章", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(CmsArticle cmsArticle)
	{
		cmsArticle.setCreateTime(new Date());
		cmsArticle.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(cmsArticleService.insertCmsArticle(cmsArticle));
	}

	/**
	 * 修改文章
	 */
	@GetMapping("/edit/{articleId}")
	public String edit(@PathVariable("articleId") Long articleId, ModelMap mmap)
	{
		CmsArticle cmsArticle = cmsArticleService.selectCmsArticleById(articleId);
		mmap.put("cmsArticle", cmsArticle);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存文章
	 */
	@RequiresPermissions("system:cmsArticle:edit")
	@Log(title = "文章", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(CmsArticle cmsArticle)
	{
		cmsArticle.setUpdateBy(ShiroUtils.getLoginName());
		cmsArticle.setUpdateTime(new Date());
		return toAjax(cmsArticleService.updateCmsArticle(cmsArticle));
	}
	
	/**
	 * 删除文章
	 */
	@RequiresPermissions("system:cmsArticle:remove")
	@Log(title = "文章", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(cmsArticleService.deleteCmsArticleByIds(ids));
	}
	
}
